package com.glory.dgcpcpda.activity.cpc;

import android.os.Build;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.AlarmStepAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.AdrefListEntity;
import com.glory.dgcpcpda.entity.AlarmStepEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AlarmStepActivity extends BaseActivity {
    // 报警代码
    @BindView(R.id.etAlarmID)
    FreeUI_ClearEditText etAlarmID;

    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;

    @BindView(R.id.rvAlarmStepList)
    RecyclerView rvAlarmStepList;

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;

    @BindView(R.id.tvStepName)
    TextView tvStepName;
    @BindView(R.id.ivStepArrow)
    ImageView ivStepArrow;
    @BindView(R.id.flStepList)
    FrameLayout flStepList;

    private AlarmStepAdapter alarmStepAdapter;

    private List<String> mStepName = new ArrayList<>();
    private FreeUI_GeneralPop mEqpTypePop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_alarm_step;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("OCAP报警查询");
        tvBaseSubmit.setText("查询");

        listTv1.setText("序号");
        listTv2.setText("步骤描述");
        listTv3.setVisibility(View.GONE);
        listTv4.setVisibility(View.GONE);

        initRecyclerView(rvAlarmStepList, this);

        // 获取工序
        getAdrefListEntity("CPCAlarmStepName");

        alarmStepAdapter = new AlarmStepAdapter(null);
        alarmStepAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        alarmStepAdapter.isFirstOnly(true);
        rvAlarmStepList.setAdapter(alarmStepAdapter);
    }

    private void getAdrefListEntity(String name) {
        FreeUI_EntityCallBack<BaseResponseBean<AdrefListEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<AdrefListEntity>>
                (new TypeToken<BaseResponseBean<AdrefListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    List<AdrefListEntity.Adref> Adreflist = response.body().getResponse().getBody().getADREFLIST();
                    if (Adreflist.size() > 0){
                        for (AdrefListEntity.Adref entity : Adreflist)  {
                            mStepName.add(entity.getKEY());
                        }
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"ADREFLISTQUERY");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("REFERENCETYPE","USER");
        map.addProperty("REFERENCENAME",name);
        NetUtils.requestPostNet("CPC.ADREFLISTQUERY","ADREFLISTQUERY",this,map,callBack);
    }

    private void getAlarmStep() {
        String stepname = tvStepName.getText().toString();
        if(StringUtils.isEmpty(stepname)){
            showToast("请选择工序！", false);
            return;
        }

        String etAlarm = etAlarmID.getText().toString();
        if(StringUtils.isEmpty(etAlarm)){
            showToast("请输入报警代码！", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<AlarmStepEntity.ALARMSTEPSPECEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<AlarmStepEntity.ALARMSTEPSPECEntity>>
                (new TypeToken<BaseResponseBean<AlarmStepEntity.ALARMSTEPSPECEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<AlarmStepEntity.ALARMSTEPSPECEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    List<AlarmStepEntity.AlarmStepSpecLine> alarmStepSpecLines = response.body().getResponse().getBody().getALARMSTEPSPEC().getAlarmStepSpecLines();
                    Collections.sort(alarmStepSpecLines, (a, b) -> a.getSeqNO().compareTo(b.getSeqNO()));
                    alarmStepAdapter.setNewData(alarmStepSpecLines);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<AlarmStepEntity.ALARMSTEPSPECEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"ADREFLISTQUERY");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERY");
        map.addProperty("STEP_NAME",stepname);
        map.addProperty("ALARM_CODE",etAlarm);
        NetUtils.requestPostNet("CPC.ALARMSTEP","TRANSFERMES",this,map,callBack);
    }


    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit, R.id.flStepList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                getAlarmStep();
                break;
            case R.id.flStepList:
                if (mStepName.size() <= 0) {
                    showToast("未获取工序列表", false);
                    return;
                }

                if (mEqpTypePop != null) {
                    if (mEqpTypePop.isShowing()) {
                        mEqpTypePop.dismiss();
                    }
                } else {
                    mEqpTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flStepList.getWidth(), mStepName, ivStepArrow, (codeS, i) -> {
                        tvStepName.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivStepArrow);
                mEqpTypePop.showAsDropDown(flStepList, 0, 0);
                break;
        }
    }
}
